package Zoo.impl;

import Zoo.entity.UserEntity;
import Zoo.service.UserService;
import Zoo.util.TableDTO;
import com.JdbcUtils_DBCP;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 * 1、用于用户的主界面（用户没有查询功能）
 * 2、用户的登录功能
 */
public class UserServiceImpl implements UserService {


    public UserServiceImpl() {
    }


    @Override
    public boolean validateUser(UserEntity userEntity) throws SQLException {
        String userName = userEntity.getUsername();
        String pwdParam = userEntity.getPwd();

        String sql = "SELECT `password` FROM`users` WHERE `name`=?";
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            //获取数据库连接
            conn = lesson05.utils.JdbcUtils_DBCP.getConnection();
            if (conn == null) {
                //如果没获取到数据库连接，则返回false
                return false;
            }
            st = conn.prepareStatement(sql);
            st.setString(1, userEntity.getUsername());
            rs = st.executeQuery();
            while (rs.next()) {
                String pwd = rs.getString(1);
                if (userEntity.getPwd().equals(pwd)) {
                    //如果获取的密码是相对应的，则返回true
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            lesson05.utils.JdbcUtils_DBCP.release(conn, st, rs);
        }


        //如果获取的密码是不匹配的，则返回false
        return false;
    }

    @Override
    public TableDTO retrieveUsers(UserEntity user, String username) {

        StringBuilder sql = new StringBuilder();
        //用户只能看到自己的个人信息
        sql.append("select * from `users` where `name` = '" + username + "'");

        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        //查出来的数据存在这里
        TableDTO returnDTO = new TableDTO();

        try {
            conn = JdbcUtils_DBCP.getConnection();
            st = conn.prepareStatement(sql.toString());
            rs = st.executeQuery();
            //查询记录
            returnDTO.setData(fillData(rs));


            return returnDTO;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils_DBCP.release(conn, st, rs);
        }

        return null;
    }

    @Override
    public boolean update(UserEntity userEntity) {
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE `users` SET `name`=?,`password`=?,`phone`=? WHERE `id`=?");

        Connection conn = null;
        PreparedStatement st = null;


        try {
            conn = com.JdbcUtils_DBCP.getConnection();
            st = conn.prepareStatement(sql.toString());
            st.setString(1, userEntity.getUsername());
            st.setString(2, userEntity.getPwd());
            st.setString(3, userEntity.getPhone());
            st.setInt(4, userEntity.getId());

            return st.executeUpdate() == 1;


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            com.JdbcUtils_DBCP.release(conn, st, null);
        }


        return false;
    }

    //存放从数据库查出来的数据
    private Vector<Vector<Object>> fillData(ResultSet rs) throws SQLException {
        Vector<Vector<Object>> data = new Vector<>();
        while (rs.next()) {
            Vector<Object> oneRecord = new Vector<>();
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String password = rs.getString("password");
            String phone = rs.getString("phone");
            oneRecord.addElement(id);
            oneRecord.addElement(name);
            oneRecord.addElement(password);
            oneRecord.addElement(phone);
            data.addElement(oneRecord);
        }
        return data;
    }

}
